package com.msmall.community.common.action.html;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.model.OperationResult;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.common.OnlineUser;
import com.msmall.community.common.service.RankRuleManager;
import com.msmall.community.score.service.ScoreManager;
import com.msmall.community.user.model.LoginInfo;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class LoginAction extends HTMLBaseAction {
	
	private static final long serialVersionUID = -1505680970768435896L;

	private String account;
	
	private String password;
	
	private String code; //验证码
	
	private String cookie;//是否自动登陆。写入cookie
	
	private UserManager userManager;

	private RankRuleManager rankRuleManager;
	
	private ScoreManager scoreManager;
	
	/**
	 * 进入完整登录页面
	 */
	public String gl(){
		return SUCCESS;
	}
	public String loginNormal(){
		OperationResult authResult = userManager.authenticate(account, password);
		if (authResult.getCode() != OperationResult.CODE_SUCCESS) {
			message = getText("sys.login.error"+authResult.getDesc());
		}else{
			UserInfo userInfo = (UserInfo) authResult.getObj();
			OnlineUser.userAuthenticated(getUserid(), userInfo.getUserid(), getSession());
			promotion(userInfo);
			addLoginScore(userInfo);
			addLoginInfo(userInfo);
			return SUCCESS;
		}
		return INPUT;
	}
	
//	/**
//	 * 检查是否是登陆用户
//	 * @return
//	 */
//	public String checkLogin(){
//		StringBuffer buffer = new StringBuffer();
//		buffer.append(ConstantAjax.RETURN_PRE);
//		if (!getIsLogin()) {
//			buffer.append(ConstantAjax.RESULT_FALSE);
//		}else{
//			buffer.append(ConstantAjax.RESULT_TRUE);
//		}
//		buffer.append(ConstantAjax.RETURN_SUFF);
//		message = buffer.toString();
//		return SUCCESS;
//	}
	/**
	 * 进入popup
	 */
	public String go(){
		return SUCCESS;
	}
	/**
	 * popup login
	 */
	public String login() {
		OperationResult authResult = userManager.authenticate(account, password);
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		if (authResult.getCode() != OperationResult.CODE_SUCCESS) {
			buffer.append(ConstantAjax.RESULT_FALSE)
			.append(ConstantAjax.CONTENT_PRE)
			.append(ConstantAjax.ITEM_PRE)
			.append(ConstantAjax.POSITION_PRE).append(1).append(ConstantAjax.POSITION_SUFF)
			.append(ConstantAjax.VALUE_PRE)
			.append(getText("sys.login.error"+authResult.getDesc()))
			.append(ConstantAjax.VALUE_SUFF)
			.append(ConstantAjax.ITEM_SUFF)
			.append(ConstantAjax.CONTENT_SUFF);
		}else{
			buffer.append(ConstantAjax.RESULT_TRUE);
			
			UserInfo userInfo = (UserInfo) authResult.getObj();
			
			buffer.append(ConstantAjax.CONTENT_PRE).append(userInfo.getNickname()).append(ConstantAjax.CONTENT_SUFF);
			
			OnlineUser.userAuthenticated(getUserid(), userInfo.getUserid(), getSession());
			
			promotion(userInfo);
			addLoginScore(userInfo);
			addLoginInfo(userInfo);
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	/**
	 * 计算是否需要升级
	 * @param userInfo
	 */
	public void promotion(UserInfo userInfo){
		boolean tag = rankRuleManager.promotion(userInfo);
		if(tag){
			userInfo.setLevels(userInfo.getLevels() + 1);
		}
	}
	/**
	 * 登录增加积分
	 * @param userInfo
	 */
	public void addLoginScore(UserInfo userInfo){
		if(ConstantDB.VIP_NORMAL_LEVELS.compareTo(userInfo.getLevels()) == 0)
			scoreManager.addNormalLogin(userInfo.getUserid(), true);
		else
			scoreManager.addVipLogin(userInfo.getUserid(), true);
	}
	
	private void addLoginInfo(UserInfo user){
		LoginInfo login = new LoginInfo();
		login.setUserid(user.getUserid());
		login.setLoginUa(getUserAgentType());
		userManager.addLoginInfo(login);
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public void setRankRuleManager(RankRuleManager rankRuleManager) {
		this.rankRuleManager = rankRuleManager;
	}
	
	public void setScoreManager(ScoreManager scoreManager) {
		this.scoreManager = scoreManager;
	}
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCookie() {
		return cookie;
	}

	public void setCookie(String cookie) {
		this.cookie = cookie;
	}
}
